<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 * */

namespace Qwadmin\Controller;

use Common\Controller\OdooApiController;

class SupplyController extends ComController {

    public function productList() {

        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = 20; #每页数量
        $where['g.status'] = 0;
        if (I('get.status')) {
            $where['g.status'] = I('get.status');
        }
        if (I('get.customer_id')) {
            $where['g.customer_id'] = I('get.customer_id');
        }
        if (I('get.goods_id')) {
            $where['g.goods_id'] = I('get.goods_id');
        }
        $count = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where($where)->count();
        $list = M('goods_supply')
                ->alias('g')
                ->join('JOIN zt_customer as c ON c.id = g.customer_id')
                ->join('JOIN zt_goods as t ON t.id = g.goods_id')->limit($offset . ',' . $pagesize)
                ->field('g.*,c.shop_name,t.name')->order('g.id DESC')->where($where)
                ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $total = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->count();
        $w_sh = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where(array('g.status' => 0))->count();
        $no_sh = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where(array('g.status' => 1))->count();
        $yes_sh = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where(array('g.status' => 2))->count();
        $this->assign('total', $total);
        $this->assign('w_total', $no_sh);
        $this->assign('no_total', $w_sh);
        $this->assign('yes_total', $yes_sh);
        $this->assign('page', $page);
        $this->assign('list', $list);
        $this->display();
    }

    public function productStatus() {
        $id = I('get.id') && preg_match('/^[0-9]+((\,[0-9]+)+)?/is', I('get.id')) ? I('get.id') : $this->error('发生了一个错误！');
        $status = !in_array(I('get.status'), array(1, 2)) ? $this->error('审核状态错误') : I('get.status');
        $note = I('get.note') ? I('get.note') : '';
        if (M('goods_supply')->where(array('id' => array('in', $id)))->save(array('status' => $status, 'note' => $note)) === flase) {
            $this->error('审核失败');
        } else {
            $goods_supply_info = M('goods_supply')->where(array('id' => array('in', $id)))->select();
            foreach ($goods_supply_info as $key => $value) {
                //更新热度值
                hotSortNumSave($value['goods_id']);
                //更新统计数据
                goods_supply_count_save( $value['id'] , 'first_stock_num,total_stock_num,residue_total_stock,use_total_stock' , $value['stock_num'] );
            }
            $this->success('审核成功');
        }
    }
    
    
    public function application(){
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $pagesize = 20; #每页数量
        $where['stock_status'] = 2;
        $where['g.application_status'] = 0;
        if (I('get.application_status')) {
            $where['g.application_status'] = I('get.application_status');
        }
        if (I('get.customer_id')) {
            $where['g.customer_id'] = I('get.customer_id');
        }
        if (I('get.goods_id')) {
            $where['g.goods_id'] = I('get.goods_id');
        }
        $count = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where($where)->count();
        $list = M('goods_supply')
                ->alias('g')
                ->join('JOIN zt_customer as c ON c.id = g.customer_id')
                ->join('JOIN zt_goods as t ON t.id = g.goods_id')->limit($offset . ',' . $pagesize)
                ->field('g.*,c.shop_name,t.name')->order('g.id DESC')->where($where)
                ->select();

        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $total = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->where(array('g.stock_status'=>2))->alias('g')->count();
        $w_sh = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where(array('g.application_status' => 0,'g.stock_status'=>2))->count();
        $no_sh = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where(array('g.application_status' => 1,'g.stock_status'=>2))->count();
        $yes_sh = M('goods_supply')->join('JOIN zt_customer as c ON c.id = g.customer_id')->join('JOIN zt_goods as t ON t.id = g.goods_id')->alias('g')->where(array('g.application_status' => 2,'g.stock_status'=>2))->count();
        $this->assign('total', $total);
        $this->assign('w_total', $no_sh);
        $this->assign('no_total', $w_sh);
        $this->assign('yes_total', $yes_sh);
        $this->assign('page', $page);
        $this->assign('list', $list);
        
        $this->display();
    }
    
    public function applicationDetail(){
        $id = I('get.id');
        if($info = M('goods_supply')->where(array('stock_status'=>2,'id'=>$id))->find()){
            $info['piclist'] = explode(',',$info['piclist']);
        
            $this->assign('info',$info);
        }else{
              $this->error('产品不存在！');
        }
       $this->display();
    }
    
    public function applicationStatus() {
        $id = I('get.id') && preg_match('/^[0-9]+((\,[0-9]+)+)?/is', I('get.id')) ? I('get.id') : $this->error('发生了一个错误！');
        $application_status = !in_array(I('get.status'), array(1, 2)) ? $this->error('审核状态错误') : I('get.status');
        $note = I('get.note') ? I('get.note') : '';
		$data_save = array('application_status' => $application_status, 'application_note' => $note);
		if( $application_status == 2 ) {
			$data_save['stock_status'] = 1;
		}
        if (M('goods_supply')->where(array('id' => array('in', $id)))->save( $data_save ) === flase) {
            $this->error('审核失败');
        } else {
            $this->success('审核成功');
        }
    }
    

}
